package com.douqu.game.core.config.common;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.goods.EquipConfig;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LogUtils;

/**
 * Created by zhenfei on 2017/7/14.
 *
 */
public class GoodsData extends EntityObject {

    public int type;

    public int id;

    public int value;

    public int[] extraValue;

    public GoodsData(){}

    public GoodsData(String value)
    {
        try {
            String[] strs = value.split(ConstantFactory.SEMICOLON);
            if(strs.length >= 3){
                this.type = Integer.parseInt(strs[0]);
                this.id = Integer.parseInt(strs[1]);
                this.value = Integer.parseInt(strs[2]);
                extraValue = new int[strs.length - 3];
                for(int i = 0; i < strs.length - 3; i++){
                    extraValue[i] = Integer.parseInt(strs[i + 3]);
                }
            }else if(strs.length == 2){
                this.type = Integer.parseInt(strs[0]);
                this.id = Integer.parseInt(strs[1]);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public GoodsData(int type, int id, int value)
    {
        this.type = type;
        this.id = id;
        this.value = value;
    }

    public GoodsData copy(){
        return new GoodsData(type, id, value);
    }

    public GoodsData loadFrom(ByteBuffer buffer){
        type = buffer.readShort();
        id = buffer.readInt();
        value = buffer.readInt();
        return this;
    }

    public void writeTo(ByteBuffer buffer){
        buffer.writeShort(type);
        buffer.writeInt(id);
        buffer.writeInt(value);
    }

    public GoodsData(int type, int value)
    {
        this(type,0,value);
    }



    public GoodsData copyNew()
    {
        GoodsData goodsData = new GoodsData();
        goodsData.type = type;
        goodsData.id = id;
        goodsData.value = value;
        return goodsData;
    }

    public SGCommonProto.GoodsObject.Builder parse()
    {
        SGCommonProto.GoodsObject.Builder builder = SGCommonProto.GoodsObject.newBuilder();
        builder.setType(type);
        builder.setId(id);
        builder.setValue(value);
        return builder;
    }


    public boolean checkAsset(Class cls, String key, int parentId)
    {
        if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.ASSET_KEY, id) == null)
        {
            LogUtils.error(cls.getSimpleName() + " Load " + key + " Error -> type : " + type + " id:" + id + " value:" + value + " parentId:" + parentId);
            return false;
        }
        return true;
    }

    public boolean checkProp(Class cls, String key, int parentId)
    {
        if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.PROP_KEY, id) == null)
        {
            LogUtils.error(cls.getSimpleName() + " Load " + key + " Error -> type : " + type + " id:" + id + " value:" + value + " parentId:" + parentId);
            return false;
        }

        return true;
    }

    public boolean check(Class cls, String key, int parentId)
    {
        if(cls.getSimpleName().equals(EquipConfig.class.getSimpleName()) && type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_EQUIP_VALUE)
            return true;

        int typeKey = 0;
        if(type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS_VALUE){
            typeKey = DataFactory.PROP_KEY;
        }else if(type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE){
            typeKey = DataFactory.ASSET_KEY;
        }else if(type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_CARDS_VALUE){
            typeKey = DataFactory.CARD_KEY;
        }

        if(typeKey == 0){
            LogUtils.error(cls.getSimpleName() + " Load " + key + " Error -> type : " + type + " id:" + id + " value:" + value + " parentId:" + parentId);
            return false;
        }

        if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(typeKey, id) == null)
        {
            LogUtils.error(cls.getSimpleName() + " Load " + key + " Error -> type : " + type + " id:" + id + " value:" + value + " parentId:" + parentId);
            return false;
        }
        return true;
    }

    public boolean checkGoods()
    {
        if(value < 0)
            return false;

        if(type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS_VALUE)
            return DataFactory.getInstance().getGameObject(DataFactory.PROP_KEY, id) != null;

        if(type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE)
            return DataFactory.getInstance().getGameObject(DataFactory.ASSET_KEY, id) != null;

        if(type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_CARDS_VALUE)
            return DataFactory.getInstance().getGameObject(DataFactory.CARD_KEY, id) != null;

        return false;
    }

    public String getGoodsStr()
    {
        int typeKey = 0;
        if(type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS_VALUE){
            typeKey = DataFactory.PROP_KEY;
        }else if(type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE){
            typeKey = DataFactory.ASSET_KEY;
        }else if(type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_CARDS_VALUE){
            typeKey = DataFactory.CARD_KEY;
        }

        if(typeKey == 0)
            return "";

        GameObject gameObject = DataFactory.getInstance().getGameObject(typeKey, id);
        return gameObject.name + "x" + value;
    }



    public SGCommonProto.RewardInfo.Builder parseCommonObject()
    {
        SGCommonProto.RewardInfo.Builder data = SGCommonProto.RewardInfo.newBuilder();
        data.setType(SGCommonProto.E_GOODS_TYPE.forNumber(type));
        data.setId(id);
        data.setNum(value);
        return data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof GoodsData)) return false;

        GoodsData goodsData = (GoodsData) o;

        if (id != goodsData.id) return false;
        if (type != goodsData.type) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = type;
        result = 31 * result + id;
        return result;
    }

    @Override
    public String toString() {
        return "{" +
                "type=" + type +
                ", id=" + id +
                ", value=" + value +
                "} ";
    }
}
